#!/usr/bin/python
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" This is a sample for testing the RASS meta resource, which is CURRENTLY STILL IN DEVELOPMENT !!!! """

import time
import rawsc
import rawsc.service
from rawsc import rass
import rawsc.rass.service
try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

# user credentials for authentication to RASS
USER = "xxx"
PWD = "xxx"
SERVER = "rass.cdn01.rambla.be"

try:
    # Create a Rass instance, passing it your login credentials and the base service uri
    client = rawsc.rass.service.RassService(username=USER, password=PWD, server=SERVER)

    # basic method for retrieving a "meta" feed, here getting all subdirs/files from the user's root dir
    resp = client.getMeta(uri = "/meta/" + USER + "/", feed_only = True)
    for e in resp.entry:
        print "\nMeta Entry URI: " + str(e.id.text)
        print "name: " + str(e.content.params.name.text)
        print "size: " + str(e.content.params.size.text)
        print "updated: " + str(e.content.params.updated.text)
        print "mimetype: " + str(e.content.params.mimetype.text)

    # since the user's rootdir is a directory, it should return a feed and not an entry
    resp = client.getMeta(uri = "/meta/" + USER + "/", entry_only = True)
    if resp is None:
        print "\nJust checking; the user's rootdir should always be a feed"
        
    # alternative method: don't pass the full URI, but only the relative dir (location on the CDN, starting from the user's rootdir)
    # Depending on the path, this method can return both a feed (= path to a dir) and an entry (= path to a file)
    resp = client.getMetaFromPath(path = "/") # add filepath here to try out
    if "feed" == str(resp._tag):
        for e in resp.entry:
            print "\nMeta Entry URI: " + str(e.id.text)
            print "name: " + str(e.content.params.name.text)
            print "size: " + str(e.content.params.size.text)
            print "updated: " + str(e.content.params.updated.text)
            print "mimetype: " + str(e.content.params.mimetype.text)
    elif "entry" == str(resp._tag):
        e = resp
        print "\nMeta Entry URI: " + str(e.id.text)
        print "name: " + str(e.content.params.name.text)
        print "size: " + str(e.content.params.size.text)
        print "updated: " + str(e.content.params.updated.text)
        print "mimetype: " + str(e.content.params.mimetype.text)
    
    # here we retrieve a feed of files, located directly below the user's rootdir
    qry = rawsc.Query()
    qry["kind"] = "file"
    resp = client.getMetaFromPath(path = "/", query = qry, feed_only = True)
    for e in resp.entry:
        print "\nMeta Entry URI: " + str(e.id.text)
        print "name: " + str(e.content.params.name.text)
        print "size: " + str(e.content.params.size.text)
        print "updated: " + str(e.content.params.updated.text)
        print "mimetype: " + str(e.content.params.mimetype.text)

except rawsc.service.Error, e:
    print "Error Response from server, contents = %s.\n" % str(e)
    error_dict = e[0] # error dict inside the first tuple elem
    print "status code : " + str(error_dict["status"]) + "\n"
    if error_dict.has_key("body"):
        print "error body : " + error_dict["body"] + "\n"
except rawsc.RawscException, e:
    print "RawscException caught, reason = %s.\n" % str(e)
except StandardError, e:
    print "StandardError caught, reason = %s.\n" % str(e)
except:
    print "unhandled exception caught\n"
